<?php
/**
 * Created by PhpStorm.
 * User: zhd
 * Date: 2019/3/13
 * Time: 20:20
 */

namespace app\Module\Index\Controller;


use app\Module\Index\Model\ArticleModel;
use app\Module\Index\Model\UnreadCommentModel;

class ArticleDetailController extends CheckAuth
{
    /**
     * 获取文章详情
     *
     * @verify int $id
     * @return string
     */
    public function getAction($id)
    {
        $articleModel = new ArticleModel();
        $articleModel->setFields("article.*,user.username,user.attestation_name,user.avatar_url,user.latitude,user.type as user_type,user.longitude,user.address_name,user.mobile,one_type.name as one_type_name,one_type.id as one_type_id,GROUP_CONCAT(DISTINCT two_type.name) as two_type_name,GROUP_CONCAT(DISTINCT two_type.id) as two_type_id,GROUP_CONCAT(DISTINCT article_image.url) as images,article_video.url as video");
        $articleModel->setJoin('article_type', 'article.id= article_type.article_id');
        $articleModel->setJoin('one_type', 'one_type.id= article_type.one_type_id');
        $articleModel->setJoin('two_type', 'two_type.id= article_type.two_type_id');
        $articleModel->setJoin('user', 'user.id= article.user_id');
        $articleModel->setJoin('article_image', 'article_image.article_id= article.id', 'LEFT JOIN');
        $articleModel->setJoin('article_video', 'article_video.article_id= article.id', 'LEFT JOIN');
        $articleModel->addWhere(['article.is_hide' => 0, 'article.id' => $id]);
        $articleModel->addGroup('article.id');
        $data = $articleModel->row();

        // 更新未读
        if ($data['user_id'] == session('userId')) {
            $unreadCommentModel = new UnreadCommentModel();
            $unreadCommentModel->addWhere('num', '0', '>');
            $unreadCommentModel->update(
                [
                    'num'     => 0,
                    'updated' => now(),
                ],
                [
                    'article_id'     => $data['id'],
                    'target_user_id' => $data['user_id'],
                ]);

        }

        if(empty($data)){
            return fail('该文章已删除');
        }else{
            return success($data);
        }
    }

    /**
     * 获取我的文章详情
     *
     * @verify int $id
     * @return string
     */
    public function getMeAction($id)
    {
        $articleModel = new ArticleModel();
        $articleModel->setFields("article.*,user.username,user.attestation_name,user.avatar_url,user.latitude,user.longitude,user.type as user_type,user.address_name,one_type.name as one_type_name,one_type.id as one_type_id,GROUP_CONCAT(DISTINCT two_type.name) as two_type_name,GROUP_CONCAT(DISTINCT two_type.id) as two_type_id,GROUP_CONCAT(DISTINCT article_image.url) as images,article_video.url as video");
        $articleModel->setJoin('article_type', 'article.id= article_type.article_id');
        $articleModel->setJoin('one_type', 'one_type.id= article_type.one_type_id');
        $articleModel->setJoin('two_type', 'two_type.id= article_type.two_type_id');
        $articleModel->setJoin('user', 'user.id= article.user_id');
        $articleModel->setJoin('article_image', 'article_image.article_id= article.id', 'LEFT JOIN');
        $articleModel->setJoin('article_video', 'article_video.article_id= article.id', 'LEFT JOIN');
        $articleModel->addWhere(['article.is_hide' => 0, 'article.id' => $id, 'user.id' => session('userId')]);
        $articleModel->addGroup('article.id');
        $data = $articleModel->row();
        return success($data);
    }
}